package com.alks.common.utils.stringUtils;

import com.alks.entity.exception.InDataException;
import jodd.util.StringUtil;

/**
 * @author MS
 * @since 2024/7/6下午4:30
 */
public class StringFormatUtils {

    /**
     * 将小写驼峰字符串改写为大写下划线
     * @param elementName 元素名
     * @return 表字段名
     */
    public static String elementToTable(String elementName) {
        if (StringUtil.isBlank(elementName)){
            return "";
        }
        /*禁止空格防止SQL注入*/
        String errElement = " ";
        if (elementName.contains(errElement)) {
            throw new InDataException("排序字段不能包含空格");
        }
        StringBuilder snakeCaseBuilder = new StringBuilder();
        for (int i = 0; i < elementName.length(); i++) {
            char currentChar = elementName.charAt(i);
            if (Character.isUpperCase(currentChar) && i > 0) {
                snakeCaseBuilder.append('_');
            }
            snakeCaseBuilder.append(Character.toUpperCase(currentChar));
        }
        return snakeCaseBuilder.toString();
    }

}
